package com.batchengine.util;

import sun.net.www.protocol.jar.JarURLConnection;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

/**
 * User: sunil.incubator@yahoo.com
 * Date: Sep 20, 2009
 * Time: 1:34:43 AM
 */
public class BatchPropertyReader {

    private static BatchPropertyReader ourInstance = null;

    static {
        ourInstance = new BatchPropertyReader();
    }

    public static BatchPropertyReader getBatchReader() {
        return ourInstance;
    }

    private BatchPropertyReader() {
    }

    public String getProperty(String key) {

        if(null == properties) {
            loadProperties();
        }
        try {
            return properties.getProperty(key);
        } catch(RuntimeException e) {
            e.printStackTrace();
            return null;
        }
    }

    Properties properties;

    private void loadProperties() {

        InputStream is = this.getClass().getResourceAsStream("/be.properties");
        if(null == is) {
            if(null == is) {
                throw new RuntimeException("property file  not found");
            }
        }
        try {
            JarURLConnection connection = new JarURLConnection(new URL(String.format("jar:%s!/be.properties", this.getClass().getProtectionDomain().getCodeSource().getLocation().toString())), null);
            properties = new Properties();
            properties.load(connection.getInputStream());
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}
